import request from "@/utils/request";

const CHAT_BASE_URL = "/api/v1";

const ChatAPI = {
  /**
   * 获取会话列表
   */
  getSessions() {
    return request({
      url: `${CHAT_BASE_URL}/customer-session/page`,
      method: "get",
    });
  },

  /**
   * 获取会话历史记录
   * @param sessionId 会话ID
   */
  getChatHistory({ sessionId, pageNum = 1, pageSize = 100 }) {
    return request({
      url: `${CHAT_BASE_URL}/chat-record/page`,
      method: "get",
      params: { sessionId, pageNum, pageSize },
    });
  },

  /**
   * 上传聊天文件
   * @param file 文件对象
   * @param sessionId 会话ID
   * @param targetId 目标用户ID
   * @param senderId 发送者ID
   */
  uploadChatFile(file, sessionId, targetId, senderId) {
    const formData = new FormData();
    formData.append("file", file);
    formData.append("sessionId", sessionId);
    formData.append("targetId", targetId);
    formData.append("senderId", senderId);

    return request({
      url: `${CHAT_BASE_URL}/chat/upload`,
      method: "post",
      data: formData,
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },

  /**
   * 获取可用客服列表
   */
  getAvailableAgents() {
    return request({
      url: `${CHAT_BASE_URL}/users/agent/available`,
      method: "get",
    });
  },

  /**
   * 更新客户会话名称
   * @param sessionId 会话ID
   * @param customerName 新的客户名称
   */
  updateSessionName(sessionId, customerName) {
    return request({
      url: `${CHAT_BASE_URL}/customer-session/${sessionId}`,
      method: "put",
      data: { customerName },
    });
  },
};

export default ChatAPI;
